package com.nowadays.mobile.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import com.nowadays.mobile.utils.FocusUtils;

import java.util.ArrayList;
import java.util.List;

public class MyFocusListView extends LinearLayout {
  private static final String androidns = "http://schemas.android.com/apk/res/android";

  private Paint paint = null;
  private List<Integer> durationList = new ArrayList<>();
  private static final float PADDING = 5.0f;

  private float hight = 20.0f;
  private float padding = PADDING;
  private Context mContext;

  public MyFocusListView(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.mContext = context;
    setWillNotDraw(false);
    init();

    String height = attrs.getAttributeValue(androidns, "layout_height");
    hight = FocusUtils.dip2px(context, Float.valueOf(height.substring(0, height.length() - 2)));
    padding = FocusUtils.dip2px(context, PADDING);
  }

  private void init() {
    paint = new Paint();
    paint.setAntiAlias(true);
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.WHITE);
  }

  public void setFocusList(List<Integer> list) {
    this.durationList = list;
    paint.setColor(Color.WHITE);
    invalidate();
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  }

  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawColor(Color.TRANSPARENT);

    float rightPoint;
    float leftPoint = 0f;

    for (int duration : durationList) {
      rightPoint = leftPoint + duration;
      RectF rectF = new RectF();
//            Log.i("MyFocusListView", "rect=" + rect + ",rightPoint=" + rightPoint + ",leftPoint= " + leftPoint);
      rectF.set(leftPoint, 0, rightPoint, hight);
      canvas.drawRect(rectF, paint);
      leftPoint = rightPoint + padding;
    }
//    invalidate();
  }

}
